﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using ZealousTTS.Web.Utility;

namespace ZealousTTS.Web
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");            
            var nameFormat = @"[\-\w\d]+";

            routes.MapRoute(
                AppConstants.RouteNames.Public.ITEM_DETAILS,
                "product/details/{id}/{productKey}", 
                new { action = "Details", controller = "Product", id = UrlParameter.Optional }, 
                new { productKey = nameFormat }, 
                new String[] {"ZealousTTS.Web.Controllers"} 
            );
            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                new String[] { "ZealousTTS.Web.Controllers" } 
            );

        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
            //RouteDebug.RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes);
            ///Configure Auto Mapper
            ZealousTTS.Web.Utility.BootStrapper.ConfigureTypeMappings();

            ZealousTTS.Web.Utility.BootStrapper.SetUserRoles();
            ZealousTTS.Web.Utility.BootStrapper.PrepareDomainUtility();
            ZealousTTS.Common.Mail.MailManager.SmtpHost = ZealousTTS.Web.Utility.ConfigReader.SmtpHost;
            ZealousTTS.Common.Mail.MailManager.SmtpPort = ZealousTTS.Web.Utility.ConfigReader.SmtpPort;
            //ZealousTTS.Web.Utility.LuceneEngine.LuceneIndexDirectory = ZealousTTS.Web.Utility.ConfigReader.LuceneIndexDirectory;
            //ZealousTTS.Web.Utility.LuceneEngine.BuildSearchIndex();
        }
    }
}